﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace FluentResourceMapping.AutoMapping
{
    public class DefaultMemberResourceMappingConvention : IMemberResourceMappingConvention
    {
        public IList<Type> ResourceTypes { get; set; }

        public DefaultMemberResourceMappingConvention()
        {
            ResourceTypes = new List<Type>();
        }

        public virtual MemberInfo MapsTo(MemberInfo property)
        {
            Type mappedType = ResourceTypes.SingleOrDefault(o => o.Name == property.DeclaringType.Name + "Resource");

            if (mappedType == null)
                return null;

            MemberInfo mappedMember = mappedType.GetMembers().SingleOrDefault(m => m.Name == property.Name);

            if (mappedMember == null)
                return null;

            return mappedMember;
        }
    }
}